import axios from 'axios'
import { MessageBox } from 'element-ui';



const instance = axios.create({
  baseURL: '/api',///挂载默认请求地址后续再请求时直接写 /admin,
 
  timeout: 8000, //响应超时时间
});

instance.interceptors.request.use((config)=>{
  
  //挂载token
  config.headers.Authorization = "Bearer "+localStorage.getItem('token')//新网址里面不叫token还要拼接字符串自己改一下
  
  return config
})

instance.interceptors.response.use((response)=>{
  
  if(response.data.code == 401){
    MessageBox.alert('登陆已过期，请重新登录','错误',{
      type:'error',
      confirmButtonText:'去登陆'
    }).then(()=>{
      localStorage.removeItem('token')//删除已过期的token
      window.location.reload()
    }).catch(()=>{
      console.log(123);
      
    })
  }

  return response
})
export default instance